package com.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * *********************************************
 * 
 * @author Administrator
 * @FileName DateUtils.java
 * @Description时间工具类
 ********************************************** 
 */
public class DateUtils {
	public static final Logger LOG = LoggerFactory.getLogger(DateUtils.class);

	private static final long oneday = 1 * 24 * 60 * 60 * 1000L;
	private static final Date today = new Date();
	private static final Object lockObj = new Object();
	private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();

	/**
	 * 返回一个ThreadLocal的sdf,每个线程只会new一次sdf
	 * 
	 * @param pattern
	 * @return
	 */
	public static SimpleDateFormat getSdf(final String pattern) {
		ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);

		// 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
		if (tl == null) {
			synchronized (lockObj) {
				tl = sdfMap.get(pattern);
				if (tl == null) {
					// 只有Map中还没有这个pattern的sdf才会生成新的sdf并放入map
					LOG.info("put new sdf of pattern " + pattern + " to map");

					// 这里是关键,使用ThreadLocal<SimpleDateFormat>替代原来直接new
					// SimpleDateFormat
					tl = new ThreadLocal<SimpleDateFormat>() {
						@Override
						protected SimpleDateFormat initialValue() {
							LOG.info("thread: " + Thread.currentThread() + " init pattern: " + pattern);
							return new SimpleDateFormat(pattern);
						}
					};
					sdfMap.put(pattern, tl);
				}
			}
		}

		return tl.get();
	}

	/**
	 * 是否是今天
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isToday(final Date date,final int delay) {
		return isTheDay(date, today, delay);
	}

	/**
	 * 是否是指定日期
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static boolean isTheDay(final Date date, final Date day,
			final int delay) {
		if(delay < 0){
			return date.getTime() >= DateUtils.dayBegin(day, delay).getTime() - oneday * Math.abs(delay)
					&& date.getTime() <= DateUtils.dayEnd(day, delay).getTime() - oneday * Math.abs(delay);
		}else if(delay == 0){
			return date.getTime() >= DateUtils.dayBegin(day, delay).getTime()
					&& date.getTime() <= DateUtils.dayEnd(day, delay).getTime();
		}else if(delay > 0){
			return date.getTime() >= DateUtils.dayBegin(day, delay).getTime() + oneday * Math.abs(delay)
					&& date.getTime() <= DateUtils.dayEnd(day, delay).getTime() + oneday * Math.abs(delay);
		}else{
			return false;
		}
	}

	/**
	 * 获取指定时间的那天 00:00:00.000 的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date dayBegin(final Date date, final int delay) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	/**
	 * 获取指定时间的那天 23:59:59.999 的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date dayEnd(final Date date, int delay) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}
}
